﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Script.Serialization;

public partial class Admin_Ajax_PhanQuyenAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        if (Request["Methods"] != null)
        {
            switch (Request["Methods"])
            {
                case "ListChucNang":
                    Response.Write(ListSanPham());
                    Response.End();
                    break;
                case "ThongTinNguoiDung":
                    ThongTinNguoiDung();
                    Response.End();
                    break;
                case "AddNew":
                    AddNew();
                    Response.End();
                    break;
                default:
                    break;
            }
        }
    }

    public string ListSanPham()
    {
        int id = int.Parse(Request.Params["iID"].ToString());
        string body = "";
        int i = 0;
        List<ChucNangINFO> p_group = ChucNangDAL.listAll();
        Dictionary<int, string> listgroup = new Dictionary<int, string>();
        List<int> list = QuanLyDAL.QL_CN(id);
        for (int j = 0; j < p_group.Count; j++)
        {
            if (p_group[j].LoaiCha != 0) continue;
            listgroup.Add(p_group[j].MaChucNang, "<div class='list-group'>");
        }
        foreach (var parent in p_group)
        {
            if (parent.LoaiCha != 0) continue;
            foreach (var item in p_group)
            {
                if (item.LoaiCha == parent.MaChucNang) {
                    i++;
                    if (list.IndexOf(item.MaChucNang) > -1)
                    {
                        body += "<div class='checkbox list-group-item'><label for='id" + i + "' style='font-weight: bold;'><input type='checkbox' id='id" + i + "' checked='checked' name='group'  rel='" + item.LoaiCha + "' relcode=" + item.MaChucNang + " style='margin-left: 20px; margin-right: 10px;' /> " + item.TenChucNang + "</label></div>";
                    }
                    else
                    {
                        body += "<div class='checkbox list-group-item'><label for='id" + i + "' style='font-weight: bold;'><input type='checkbox' id='id" + i + "' name='group'  rel='" + item.LoaiCha + "' relcode=" + item.MaChucNang + " style='margin-left: 20px; margin-right: 10px;' /> " + item.TenChucNang + "</label></div>";
                    }
                }
            }
            listgroup[parent.MaChucNang] += body + "</div>";
            body = "";
        }
        StringBuilder json = new StringBuilder();
        json.Append("[");
        i = 1;
        foreach (var parent in p_group)
        {
            if (parent.LoaiCha != 0) continue;
            json.Append("{\"GID\":" + parent.MaChucNang + ",\"Data\":\"" + listgroup[parent.MaChucNang] + "\"}");
            if (i < listgroup.Count) json.Append(",");
            i++;
        }
        json.Append("]");
        return json.ToString();
    }

    private void ThongTinNguoiDung()
    {
        int id = int.Parse(Request.Params["iID"].ToString());
        QuanLyINFO obj = QuanLyDAL.GetInfo(id);
        JavaScriptSerializer serial = new JavaScriptSerializer();
        Response.Write(serial.Serialize(obj));
        Response.End();
    }

    public void AddNew()
    {
        int MaQL = int.Parse(Request.Params["Ma"]);
        string[] para = Request.Params.GetValues("group");
        List<int> dsCN = new List<int>();
        for (int i = 0; i < para.Length; i++)
		{
			dsCN.Add(int.Parse(para[i])); 
		}
        if (QuanLyDAL.Them_CN_QL(MaQL, dsCN) > 0)
        {
            Response.Write("{\"Status\": \"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\": \"Error\",\"Mess\":\"Không thể phân quyền.\"}");
        }
    }   
}